package apiexample.transform;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.SpringLayout.Constraints;

import com.apiexample.server.dao.ClassDao;
import com.apiexample.server.dao.ExampleDao;
import com.apiexample.server.dao.MethodDao;
import com.apiexample.server.entity.ClassEntity;
import com.apiexample.server.entity.MethodEntity;

import dao.APIDao;
import dao.PageDao;
import dao.QueryDao;
import dao.entity.PageEntity;
import dao.entity.QueryEntity;

public class CentralDataPool {
//	public static Connection  apiexample_conn = null;
//
//	public static Connection apiserver_conn = null;
	// map of class name-->id in apiserver database
	public static HashMap<String, Integer> classNameMapID;

	// map of api id-->name in apiexample database
	public static HashMap<Integer, String> apiIdMapName;
	// map of method name-->id in apiserver database
	public static HashMap<String, Integer> methodNameMapID;

	public static HashMap<String, PageEntity> linkMapPage;

	public static HashMap<Integer, String> idMapLink;

	public static ArrayList<QueryEntity> allQueries;

	public static void persist (final Connection  apiexample_conn, final Connection apiserver_conn){
		if(apiexample_conn==null || apiserver_conn==null){
			System.err.println("Database connection is null!");
			return;
		}
		
		System.out.println("Start to persist data....");		
		
		classNameMapID = ClassDao.getNameMapID(apiserver_conn);
		
		System.out.println("1");

		apiIdMapName = APIDao.getIDMapName(apiexample_conn);
		
		System.out.println("2");

		methodNameMapID = new HashMap<String, Integer>();
		ArrayList<MethodEntity> allMethods = MethodDao
				.getAllMethodList(apiserver_conn);
		ArrayList<ClassEntity> allClasses = ClassDao
				.getAllClassList(apiserver_conn);
		HashMap<Integer, String> classIdMapName = new HashMap<Integer, String>();
		for (ClassEntity classEntity : allClasses) {
			classIdMapName.put(new Integer(classEntity.getId()), classEntity
					.getName());
		}
		for (MethodEntity methodEntity : allMethods) {
			String className = classIdMapName.get(methodEntity.getClass_id());
			if (className == null) {
				continue;
			} else {
				// methodName format: className_methodName_inputtype
				// fieldName format: className-fieldName
				// judge whether this item is a method or a field, field's input type is a special symbol: #
				if(methodEntity.getInput().equals("#")){
					//a field
					methodNameMapID.put(className+"-"+methodEntity.getName(), methodEntity.getId());
				}else{
					//a method
					methodNameMapID.put(className+"_"+methodEntity.getName()+"_"+methodEntity.getInput(), methodEntity.getId());
				}
			}
		}
		System.out.println("3");
		linkMapPage = PageDao
		.getLinkMapPage(apiexample_conn, PageEntity.EXTRACT_CODE_OK);
		System.out.println("4");
		idMapLink = PageDao.getIDMapLink(apiexample_conn);
		System.out.println("5");
		allQueries = QueryDao.getAllQuery(apiexample_conn);
		System.out.println("6");
		exampleIDBase = ExampleDao.getMaxID(apiserver_conn);
		System.out.println("End of persist data....");
	}

	private static int index = 0;

	public static synchronized QueryEntity getNextQuery() {
		if (index >= allQueries.size()) {
			return null;
		}
		QueryEntity next = allQueries.get(index++);
//		System.out.println("Processing " + index + " queries");
		return next;
	}

	private static int exampleIDBase;
	/**
	 * Generate the example id for each thread
	 * 
	 * @return int
	 */
	public static synchronized int generateExampleID() {
		return (++exampleIDBase);
	}
}
